/**
 @description 地图功能 
 @author ffpan
 @requires  googlemap api  (http://ditu.google.cn/maps?file=api&amp;v=2.x&amp;key=ABQIAAAAWlfiFVYKHX8tCx74bFRnfhRpp30BDXp-1xIerK7XpUs0bSzu7xSVrUD63jB2YX5cVd1HtH12nrekzg&hl=zh-CN)
 @version 1.0
*/

MyGoogleMap = function(){
	this.initialize();
}

MyGoogleMap.prototype = {
	
	/** 初始化地图 **/
	initialize : function(){
		/**初始化参数 **/
		this.mapDiv = document.getElementById("map_canvas");

		if (GBrowserIsCompatible()) {
			this.map = new GMap2(this.mapDiv);
			this.map.setCenter(new GLatLng(31.979, 120.894), 12);
			this.geocoder = new GClientGeocoder();
			this.map.addControl(new GSmallMapControl());
		}
	},
	
	/** 显示地图 **/
	drawMap : function(){
	
	},

	/** 生成icon *****/
	getIcon : function(){
		var icon = new GIcon();
		icon.image = "_skins/tplimgs/mm.png";
		icon.iconSize = new GSize(48, 40);
		icon.iconAnchor  = new GPoint(5,5);
		return icon;
	},
	
	/** 根据地名返回经纬度 **/
	getPointer : function(address , number){
		var  _this = this;
		if (this.geocoder) {
			this.geocoder.getLatLng( address , function(point) {
				if(!point){point="unknown";}
				document.title = "lat:"+point.y+"--lng:"+point.x;
				_this.map.setCenter(point, 13);
				
				//_this.map.addOverlay(new GMarker(point));

				//var hicon = _this.getIcon();
				//var marker = new GMarker(point , {icon : hicon , dragCrossMove:true , title:"hello" , draggable:true});
				//_this.map.addOverlay(marker);

				var new_marker=new KMarker(new GLatLng(Number(point.y) ,  Number(point.x)), "", number+"套" ,  {"index": 1});
				_this.map.addOverlay(new_marker);

		});
      }
	},
	
	/***根据经纬度划出浮标****/
	setPointer : function(pid ,address , number , lat , lng){
		var new_marker=new KMarker(new GLatLng(Number(lat) ,  Number(lng)),  address , number+"套" , pid ,  {"index": 1});
		this.map.addOverlay(new_marker);
	},

	/** 将对应的名称和经纬度提交到后台页面 **/
	postData : function(point){
		var data = {
			name : $("#name").attr("value"), 
			address : $("#address").attr("value"),
			pLat : point.y,
			pLng : point.x
		}
		var url = "map.php";
		$.get(url , data, function(txt){
			alert("Data Loaded: " + txt);
		}); 
	}
}

/*****************新增KMarker*****************/
var _index = 1;
var KMarker = function(latlng, tip, markerContent, pid , clickParamer){
	this._latlng = latlng;
	this._tip = tip;
	this._markerContent = markerContent;
	this._clickParamer = clickParamer;
	this._pid = pid;
}

KMarker.prototype = new GOverlay();

KMarker.prototype.initialize = function(map_) {
    var markerHtml =document.createElement('div');
    markerHtml.setAttribute("index", this._clickParamer["index"]);
    markerHtml.innerHTML = this._markerContent;
	markerHtml.className = "marker";
	markerHtml.setAttribute("maptips" , this._tip);
	markerHtml.setAttribute("pid" , this._pid);
    map_.getPane(G_MAP_MARKER_PANE).appendChild(markerHtml);

    var markerTip = document.getElementById("maptips");

	this._markerHtml = markerHtml;
    this._markerTip = markerTip;
    this._map = map_;
   
	var _this = this;
	 //map_.getPane(G_MAP_MARKER_PANE).appendChild(markerTip);

    GEvent.addDomListener(markerHtml, "mouseover", function() {
		 _index++;
        _this._markerTip.style.zIndex = _index;
        markerHtml.style.zIndex = _index;
		
		markerHtml.className = "marker marker_select";
		markerHtml.style.cursor = "pointer";

		_this._markerTip.innerHTML = markerHtml.getAttribute("maptips");
		_this._markerTip.style.left = (posX(markerHtml)+46)+"px";
		_this._markerTip.style.top = posY(markerHtml)+"px";

		_this._markerTip.style.display = "";

    });

	 GEvent.addDomListener(markerHtml, "mouseout", function() {
		 _index--;
        _this._markerTip.style.zIndex = _index;
        markerHtml.style.zIndex = _index;
		
		markerHtml.className = "marker";
		_this._markerTip.style.display = "none";
    });

	GEvent.addDomListener(markerHtml, "click", function() {
		var plaza = markerHtml.getAttribute("maptips");
		$("#h_mapplaza").val(plaza);
		ajaxResult();
    });

};

KMarker.prototype.remove = function() {
    this._markerHtml.parentNode.removeChild(this._markerHtml);
    this._markerTip.parentNode.removeChild(this._markerTip);
};
KMarker.prototype.copy = function() {
    return new (this._latlng,this._tip,this._markerContent,this._clickParamer);
};
KMarker.prototype.redraw = function(force) {
    if (force){
        var p = this._map.fromLatLngToDivPixel(this._latlng);
        this._markerHtml.style.left = (p.x) + "px";
        this._markerHtml.style.top =  (p.y)  + "px";
        this._markerTip.style.left = (p.x)  + "px";
        this._markerTip.style.top = (p.y)  + "px";
    }
};
 
KMarker.prototype.getMarkerHtml = function() {
    return this._markerHtml;
}
 
KMarker.prototype.getMarkerTip = function() {
    return this._markerTip;
}


/************初始化地图图标*********************/





/*************************************************/

function posX(e){
		var x = e.offsetLeft;
		if(e.offsetParent!=null)	x+=posX(e.offsetParent);
		return x;
}

function posY(e){
	var y = e.offsetTop;
	if(e.offsetParent!=null)	y+=posY(e.offsetParent);
	return y;
}

function showList(list){
	$("#map_window").css("display" , "");
	
	
	var tablebody=0;

	if(list.length==0){
		$("#noprops").css("display" , "");
		$("#property_list").css("display" , "none");
	}else{
		var linkurl = $("#plazatype").val()==0?"sellhouse_detail.php?sid=" : "renthouse_detail.php?rid=";
		var tips = $("#plazatype").val()==0?"万":"元/月";
		var titletips = $("#plazatype").val()==0?"售价":"租金";
		var tableheader = "<table cellspacing=\"0\" cellpadding=\"2\" border=\"0\" ><tr class=\"title\"><th width=\"60\" align=\"left\">&nbsp;编号</th><th width=\"190\" align=\"left\">小区&nbsp;房型</th><th width=\"60\" align=\"left\">楼层</th><th width=\"70\" align=\"left\">面积</th><th width=\"80\" align=\"left\">"+titletips+"</th><th width=\"100\"></th></tr>";
		
	
		var arr = [];
		var str ="";
		for(var i=0,l=list.length;i<l;i++){
			var info = list[i];
			var p = "";
		/*	if(info["prop"]==0){
				p = "<a href='agent_detail?aid="+info["aid"]+"' title='看看这家中介的其他房源'>"+info["publisher"]+"</a>";
			}else{
				p= info["publisher"];
			}*/
			str = "<tr><td>&nbsp;<a href='"+linkurl+info["id"]+"' target='blank'>"+info["infoid"]+"</a></td><td><a href='"+linkurl+info["id"]+"' target='blank'>"+info["plaza"]+"&nbsp;"+info["style_detail"]+"</a></td><td>"+info["floor_detail"]+"</td><td>"+info["square"]+"㎡</td><td><span class='orangewm'>"+info["price"]+tips+"</span></td><td><span class='grayw'>"+info["postdate"]+"</span></td></tr>";
		/*	str =   "<tr class='col"+i%2+"'><td>&nbsp;&nbsp;<a href='sellhouse_detail.php?sid="+info["id"]+"'>"+info["title"]+"</a></td><td><span class='con'>"+info["square"]+"</span><span class='tips'>㎡</span></td><td align='left'><span class='con'>"+info["floor"]+"</span><span class='tips'></span></td><td><span class='con1'>"+info["price"]+"万</span></td>	<td>"+p+"</td>	<td align=\"center\"><span style=\"color:#666;\">"+info["postdate"]+"</span></td></tr>";*/
			arr.push(str);
		}
		tablebody = arr.join("");
		var tablefoot = "</table>";

		$("#property_list").html(tableheader+tablebody+tablefoot);
		$("#noprops").css("display" , "none");
		$("#property_list").css("display" , "");
	}
	
}

function updateCondition(opt){
	$.each(opt , function(index){
		window.condition[index] = opt[index];
	});
}


function ajaxResult(plaza){
	var ajax_data = {
		"plaza" : $("#h_mapplaza").val(),
		"pt" : $("#plazatype").val(),
		"order":$("#h_maporder").val(),
		"page" : $("#h_mappage").val(),
		"style" : $("#h_mapstyle").val(),
		"price" : $("#h_mapprice").val()
	};
	var url = "ajax_house_list.php";
	$.get(url , ajax_data , function(json){
		var json = eval(json);
		showList(json);
	});
	/*document.forms[0].submit();*/
	/*var url = "ajax_sellhouse_list.php";
	updateCondition(data);
	$.get(url , window.condition , function(json){
		var json = eval(json);
		showList(json);
	});*/
}

$(document).ready(function(){
	var newMap = new MyGoogleMap();
	
	var url = "ajax_get_plaza.php";
	var data = {
		pl : $("#plazalist").val(),
		pt : $("#plazatype").val()
	};
	$.get(url , null , function(json){
		json = eval(json);
		$.each(json, function(index) { 
			var address=json[index]["name"];
			var number = $("#plazatype").val()==0?json[index]["snumber"]:json[index]["rnumber"];
			var lat = json[index]["lat"];
			var lng = json[index]["lng"];
			var pid =  json[index]["id"];
			newMap.setPointer(pid , address , number , lat ,lng);
		});
	});
	
	$("#map_window_close").bind("click" , function(){
		$("#map_window").css("display" , "none");
	});
	
	$("#map_sort").bind("change" , function(){
		$("#h_maporder").val($(this).val());
		ajaxResult();
	});
	
	$("#map_filter_price").bind("click" , function(e){
		var el = e.target;
		if(el.tagName=="A"){
			val = $(el).attr("value");
			$("#h_mapprice").val(val);
		}
		ajaxResult();
	});
	
	$("#map_filter_style").bind("click" , function(e){
		var el = e.target;
		if(el.tagName=="A"){
			val = $(el).attr("value");
			$("#h_mapstyle").val(val);
		}
		ajaxResult();
	});
});

window.unload = function(){
	GUnload();
}

window.condition = {};
